import matplotlib.pyplot as plt
import operator
import networkx as nx
import csv
import numpy as np
from datetime import datetime
from time import strftime

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    f = open(inputCSV)
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.DictReader(f, dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = int(float(record[item]))
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def ctgdicbuilder(tspContiguity):
    # build a dictionary for contiguity matrix
    # tspContiguity: a ctg file data ready by build_data_list
    tid = np.unique(tspContiguity[:,0])
    ctgdic = {}

    for item in tid:
        ctgdic[item] = []

    for item in tspContiguity:
        if item[0] <> item[1]:
            ctgdic[item[0]].append(item[1])
            
    return ctgdic

def getnetflow(fdatalist, numunit):
    # return netfdatamatrix: [i,j], from i to j
    fdatamatrix = np.zeros((numunit,numunit));
    #netfdatamatrix = np.zeros((numunit,numunit));
    netfdatalist = []
    for item in fdatalist:
        fdatamatrix[int(item[0]), int(item[1])] = item[2]
    
    #for i in range(numunit-1):
        #for j in range(i+1,numunit):
            #tin = fdatamatrix[j, i]
            #tout = fdatamatrix[i, j]
            #if tin > tout:
                #netfdatamatrix[j,i] = tin - tout
            #else:
                #netfdatamatrix[i,j] = tout - tin
    
    for i in range(numunit):
        for j in range(numunit):
            if i <> j:
                tnet = int(fdatamatrix[i, j] - fdatamatrix[j, i]) # out - in
                if tnet > 0:
                    #netfdatamatrix[i,j] = tnet
                    netfdatalist.append([i,j,tnet])
        
    #return netfdatamatrix
    return netfdatalist
    
                

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    print '---------------------------------------------------------'
    #filepath = 'C:/_DATA/migration_census_2000/STATE/'
    #contiguityCSV = filepath + 'continental_cnty_state.shp.ctg'
    #spContiguity = build_data_list(contiguityCSV)
    #ctgdic = ctgdicbuilder(spContiguity)
    #G = nx.DiGraph(ctgdic)
    '''
    filepath = 'C:/_DATA/migration_census_2000/'
    cntymigdataCSV = filepath + 'census_county_migration_format.csv'
    cntymigdata = build_data_list(cntymigdataCSV)
    fileLoc = cntymigdataCSV[:-4] +'.networkx.txt'
    np.savetxt(fileLoc, cntymigdata, delimiter='\t', fmt = '%s')
    cntymigG = nx.read_weighted_edgelist(fileLoc, create_using=nx.DiGraph())
    '''
    
    filepath = 'C:/_DATA/migration_census_2000/STATE/'
    migrationdataCSV = filepath + 'census_migration_state.csv'
    migrationdata = build_data_list(migrationdataCSV)
    netmigrationdata = getnetflow(migrationdata, 49)
    fileLoc = migrationdataCSV[:-4] +'.networkx.txt'
    np.savetxt(fileLoc, netmigrationdata, delimiter='\t', fmt = '%s')
    statemigG = nx.read_weighted_edgelist(fileLoc, create_using=nx.DiGraph())
    
    '''
    filepath = 'C:/_DATA/migration_census_2000/REDCAPLUS_100Region/'
    migrationdataCSV = filepath + 'census_migration_100regs.csv'
    migrationdata = build_data_list(migrationdataCSV)
    netmigrationdata = getnetflow(migrationdata, 100)
    fileLoc = migrationdataCSV[:-4] +'.networkx.txt'
    np.savetxt(fileLoc, netmigrationdata, delimiter='\t', fmt = '%s')
    regmigG = nx.read_weighted_edgelist(fileLoc, create_using=nx.DiGraph())
    '''
    usedG = statemigG
    N,K = usedG.order(), usedG.size()
    avg_deg = float(K)/N
    print "Nodes: ", N
    print "Edges: ", K
    print "Average degree: ", avg_deg
    #nx.draw(usedG)
    #degrees = usedG.degree()
    degrees = []
    for i in range(N):
        degrees.append([i, usedG.in_degree(str(i)), usedG.out_degree(str(i))])
    #n, bins, patches = plt.hist(usedG.out_degree().values(), 15, normed = 1, facecolor = 'green', alpha = 0.75)
    #plt.show()
    fileLoc = filepath +'degrees.csv'
    headerstr = 'ffid,indegree,outdegree'
    np.savetxt(fileLoc, degrees, delimiter=',', header = headerstr, fmt = '%s')
    
    #usedG_ud = usedG.to_undirected()
    #clust_coefficients = nx.clustering(usedG_ud)
    #print max(clust_coefficients.iteritems(), key=operator.itemgetter(1))
    #inverse = [(value, key) for key, value in clust_coefficients.items()]
    #sorted_clust_coefficients = sorted(clust_coefficients.iteritems(), key=operator.itemgetter(1))
    '''
    clust_coefficients_list = np.zeros(N)
    for key, value in clust_coefficients.items():
        clust_coefficients_list[int(key)] = value
    fileLoc = filepath +'temp.csv'
    np.savetxt(fileLoc, clust_coefficients_list, delimiter=',', fmt = '%s')
    '''
    # print all the edges with weights
    '''
    for n,nbrs in G.adjacency_iter():
        for nbr,eattr in nbrs.items():
            data=eattr['weight']
            #print type(n),nbr,data
            if data>0: print n,nbr,data
    '''

    
    #length = nx.all_pairs_dijkstra_path_length(G)
   